package com.yogee.topseller.activity.my;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.yogee.core.base.HttpActivity;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.AppManager;
import com.yogee.core.utils.SharedPreferencesUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.login.view.impl.ForgetPswActivity;
import com.yogee.topseller.activity.login.view.impl.GuideLoginRegist;
import com.yogee.topseller.bean.GetPlusBean;
import com.yogee.topseller.bean.ResultBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.utils.LogUtils;
import com.yogee.topseller.view.BasicDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class SettingActivity extends HttpActivity {

    @BindView(R.id.title_white_left_back)
    LinearLayout titleWhiteLeftBack;
    @BindView(R.id.title_white_title)
    TextView titleWhiteTitle;
    @BindView(R.id.rl_about_us)
    RelativeLayout rlAboutUs;
    @BindView(R.id.tv_current_verson)
    TextView tvCurrentVerson;
    @BindView(R.id.setting_push_iv)
    ImageView pushIv;
    private String isPlus = "0";
    private String cid;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {
        cid = (String) SharedPreferencesUtils.get(this, "cid", "0");
        titleWhiteTitle.setText("设置");
        getVersion();
        getPlus(AppUtil.getUserId(this));
    }

    private void getPlus(String userId) {
        HttpManager.getInstance().getPlus(userId, cid).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<GetPlusBean>() {
            @Override
            public void onNext(GetPlusBean o) {
                isPlus = o.getIsPlus();
                if (isPlus.equals("1")) {
                    pushIv.setImageResource(R.drawable.select1);
                } else {
                    pushIv.setImageResource(R.drawable.unselect1);
                }
            }
        }, this));
    }

    private void setPlus(String userId, final String isPlus) {
        HttpManager.getInstance().setPlus(cid, userId, isPlus).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<ResultBean>() {
            @Override
            public void onNext(ResultBean o) {
                if (o.getResult().equals("1")) {
                    if (isPlus.equals("1")) {
                        pushIv.setImageResource(R.drawable.select1);
                        SettingActivity.this.isPlus = "1";
                    } else {
                        pushIv.setImageResource(R.drawable.unselect1);
                        SettingActivity.this.isPlus = "0";
                    }
                }
            }
        }, this));
    }

    @OnClick({R.id.title_white_left_back, R.id.rl_about_us, R.id.tv_loginout, R.id.setting_push_rl})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_white_left_back:
                finish();
                break;
            case R.id.rl_about_us:
                startActivity(new Intent(SettingActivity.this, AboutUsActivity.class));
                break;
            case R.id.tv_loginout://退出登录
                showNormalDialog();
                break;
            case R.id.setting_push_rl://消息推送
                if (isPlus.equals("0")) {
                    setPlus(AppUtil.getUserId(SettingActivity.this), "1");
                } else {
                    setPlus(AppUtil.getUserId(SettingActivity.this), "0");
                }
                break;
        }
    }

    private void getVersion() {
        try {
            PackageManager pm = SettingActivity.this.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(SettingActivity.this.getPackageName(), 0);
            tvCurrentVerson.setText("当前版本 " + pi.versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void showNormalDialog() {
        BasicDialog.Builder builder = new BasicDialog.Builder(this);
        builder.setMessage("是否退出登录？")
                .setPositiveButton("确定", new android.app.AlertDialog.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        MobclickAgent.onEvent(SettingActivity.this, "loginout_btn");
                        AppUtil.clearUserInfo(SettingActivity.this);
                        SharedPreferencesUtils.clear(SettingActivity.this);
                        AppManager.finishAllActivity();

                        startActivity(new Intent(SettingActivity.this, GuideLoginRegist.class).putExtra("isSetting", "1"));
                        finish();
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("取消", new android.app.AlertDialog.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

        builder.create().show();
    }

    @Override
    public void showMsg(String msg) {
        super.showMsg(msg);
        LogUtils.e("test111111111",msg);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this); //统计时长
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}